//
//  CustomerInfoView.m
//  mendianjieke
//
//  Created by 响当当 on 7/31/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "CustomerInfoView.h"
#import "CustomerHeadView.h"
#import "CustomerInfoCell.h"
#import "AttackCell.h"

@interface CustomerInfoView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UIButton *customerButton;
@property (nonatomic, strong)UIButton *attackButton;
@property (nonatomic, strong)CustomerHeadView *customerHeadView;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, copy)NSArray *infoArr;

@property (nonatomic,strong) NSMutableArray *attackArr;
@property (nonatomic,assign) NSInteger index;

@property (nonatomic, strong)NSDictionary *customerDic;
@property (nonatomic, strong)NSDictionary *customerAttackDic;
@property (nonatomic, strong)CustomerInfoModel *model;
@property (nonatomic,strong) NSMutableArray *dataArray;

@end

@implementation CustomerInfoView

- (NSDictionary *)customerDic
{
    if (!_customerDic)
    {
        _customerDic = [[NSDictionary alloc]init];
    }
    
    return _customerDic;
}
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}

- (NSMutableArray *)attackArr {
    if (!_attackArr) {
        _attackArr = [[NSMutableArray alloc]init];
    }
    return _attackArr;
}

- (NSArray *)infoArr
{
    if (!_infoArr)
    {
        _infoArr = [[NSArray alloc]initWithObjects:self.model.gender.stringValue,self.model.age.stringValue,self.model.joinTime.stringValue,self.model.maxCost.stringValue,self.model.minCost.stringValue,self.model.avgCost.stringValue,self.model.buyingCycle.stringValue,self.model.lifeCycle,nil];
    }
    return _infoArr;
}

- (UIButton *)customerButton
{
    if (!_customerButton)
    {
        _customerButton = [Utilties creatButtonWithFrame:CGRectMake(0,0,187*kUIScaleBasePlus, 45*kUIScaleBasePlus)
                                            textAligment:UIControlContentHorizontalAlignmentCenter
                                                    text:@"顾客信息"
                                                textClor:JSAMainColor
                                               buttonBGC:[UIColor whiteColor]
                                                textFont:15*kUIScaleBasePlus
                                                  target:self
                                                  method:@selector(segmentClick:)];
        _customerButton.tag = 0;
    }
    return _customerButton;
}

- (UIButton *)attackButton
{
    if (!_attackButton)
    {
        _attackButton = [Utilties creatButtonWithFrame:CGRectMake(CGRectGetMaxX(self.customerButton.frame),0,188*kUIScaleBasePlus, 45*kUIScaleBasePlus)
                                            textAligment:UIControlContentHorizontalAlignmentCenter
                                                    text:@"攻略"
                                                textClor:LCHexColor(0x282828)
                                               buttonBGC:[UIColor whiteColor]
                                                textFont:15*kUIScaleBasePlus
                                                  target:self
                                                method:@selector(segmentClick:)];
        _attackButton.tag = 1;
    }
    return _attackButton;
}

- (CustomerHeadView *)customerHeadView
{
    if (!_customerHeadView)
    {
        _customerHeadView = [[CustomerHeadView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,240*kUIScaleBasePlus)];
        _customerHeadView.backgroundColor = [UIColor whiteColor];
    }
    return _customerHeadView;
}


- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,50*kUIScaleBasePlus,SCREEN_WIDTH,SCREEN_HEIGHT-NAVIBAR_HEIGHT-50*kUIScaleBasePlus) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableHeaderView = self.customerHeadView;
        _tableView.showsVerticalScrollIndicator = NO;

        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        
        // 设置文字
        [header setTitle:@"加载更多" forState:MJRefreshStateIdle];
        [header setTitle:@"下拉刷新" forState:MJRefreshStatePulling];
        [header setTitle:@"Loading ..." forState:MJRefreshStateRefreshing];
        
        // 设置字体
        header.stateLabel.font = [UIFont systemFontOfSize:17];
        header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:14];
        
        // 设置颜色
        header.stateLabel.textColor = LCHexColor(0x1f1f1f);
        header.lastUpdatedTimeLabel.textColor = [UIColor clearColor];
        
        // 设置刷新控件
        self.tableView.mj_header = header;
        
        //上拉刷新
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadNewFootData)];
        //设置上拉刷新属性
        [footer setTitle:@"加载更多" forState:MJRefreshStateIdle];
        [footer setTitle:@"Loading ..." forState:MJRefreshStateRefreshing];
        
        // 设置字体
        footer.stateLabel.font = [UIFont systemFontOfSize:17];
        
        // 设置颜色
        footer.stateLabel.textColor = LCHexColor(0x1f1f1f);
        
        //        _tableView.mj_footer = footer;
    }
    return _tableView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self addSubview:self.customerButton];
        [self addSubview:self.attackButton];
        [self addSubview:self.tableView];
    }
    return self;
}

#pragma mark - delegate & dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.index == 0)
    {
        return self.dataArray.count;
    }
    else
    {
        return self.attackArr.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.index == 0)
    {
        TableViewModel *model = self.dataArray[indexPath.row];
        return model.cellHeight;
    }
    else
    {
        return 75*kUIScaleBasePlus;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *backcell = nil;
    
    if (self.index == 0)
    {
        CustomerInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomerInfoCell"];
        if (!cell)
        {
            cell = [[CustomerInfoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomerInfoCell"];
            cell.selectionStyle = 0;
        }
        
        cell.model = self.dataArray[indexPath.row];
        backcell = cell;
    }
    else
    {
        AttackCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AttackCell"];
        if (!cell)
        {
            cell = [[AttackCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AttackCell"];
            cell.selectionStyle = 0;
        }
        
        cell.model = self.attackArr[indexPath.row];
        backcell = cell;
    }
   
    
    return backcell;
}

#pragma mark - Method
- (void)loadNewData
{
    WeakSelf(self)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf.tableView.mj_header endRefreshing];
    });
}

- (void)loadNewFootData
{
    WeakSelf(self)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [weakSelf.tableView.mj_footer endRefreshing];
        
    });
    
}

- (void)segmentClick:(UIButton *)sender
{
    self.index = sender.tag;
    if (sender.tag == 0)
    {
        [self.customerButton setTitleColor:JSAMainColor forState:UIControlStateNormal];
        [self.attackButton setTitleColor:LCHexColor(0x282828) forState:UIControlStateNormal];
    }
    else
    {
        [self.customerButton setTitleColor:LCHexColor(0x282828) forState:UIControlStateNormal];
        [self.attackButton setTitleColor:JSAMainColor forState:UIControlStateNormal];
    }
    
    if (self.index == 0)
    {
        self.tableView.tableHeaderView = self.customerHeadView;
    }
    else
    {
        self.tableView.tableHeaderView = [UIView new];
        self.tableView.tableHeaderView.height = 0.1;
    }
    
    [self.tableView reloadData];
}


- (void)getInfo:(NSDictionary *)dic
{
    self.customerDic = dic;
    self.model = [[CustomerInfoModel alloc]initWithDic:dic[@"summary"]];
    
    NSMutableArray *dataArr = [[NSMutableArray alloc]init];
    
    NSDictionary *dics = [NSDictionary dictionaryWithDictionary:self.customerDic[@"timeAxis"]];
    
    [dataArr addObject:[NSString stringWithFormat:@"%@ 通过活动入会",[Utilties getTime:[dics[@"1"] stringValue]]]];
    
    NSArray *dataArrs = dics[@"2"];
    NSString *str = [NSString stringWithFormat:@"购买频次最多的产品：%@;%@",dataArrs[0],dataArrs[1]];
    [dataArr addObject:str];
    
    NSArray *dataArrss = dics[@"3"];
    NSString *strs = [NSString stringWithFormat:@"最短与最长购物间距：%@/%@",dataArrss[0],dataArrss[1]];
    [dataArr addObject:strs];
    
    NSNumber *times = dics[@"4"];
    NSString *strsss = [NSString stringWithFormat:@"会员中心交互次数：%@",times.stringValue];
    [dataArr addObject:strsss];
    
    NSDictionary *dicss = dics[@"5"];
    NSNumber *time = dicss[@"time"];
    NSNumber *inShop = dicss[@"in"];
    NSNumber *outShop = dicss[@"out"];
    NSArray *detailArr = dicss[@"detail"];
    NSString *strss = [NSString stringWithFormat:@"%@,上上次来店时间：,%@%@,%@,%ld",inShop.stringValue,[Utilties getTime:time.stringValue],detailArr.count==0 ? @"无消费":@"有消费",outShop.stringValue,detailArr.count];
    [dataArr addObject:strss];
    
    NSDictionary *dicsss = dics[@"6"];
    NSNumber *timesss = dicsss[@"time"];
    NSNumber *inShops = dicss[@"in"];
    NSNumber *outShops = dicss[@"out"];
    NSArray *detailArrs = dicss[@"detail"];
    NSString *stra = [NSString stringWithFormat:@"%@,上次来店时间：,%@%@,%@,%ld",inShops.stringValue,[Utilties getTime:timesss.stringValue],detailArrs.count==0 ? @"无消费":@"有消费",outShops.stringValue,detailArrs.count];
    [dataArr addObject:stra];
    
    
    for (int i = 0; i < dataArr.count; i ++) {
        TableViewModel *model = [[TableViewModel alloc]init];
        model.text = dataArr[i];
        model.cellHeight = [CustomerInfoCell heightWithModel:model];
        if (dataArr.count-1 == i)
        {
            model.lastNum = [NSString stringWithFormat:@"%d",i];
        }
        else
        {
            model.num = [NSString stringWithFormat:@"%d",-1];
        }
        
        [self.dataArray addObject:model];
    }
    
    [self.customerHeadView getInfo:self.infoArr headImage:dic[@"photo"]];
    [self.tableView reloadData];
}

- (void)customerAttack:(NSDictionary *)attackDic
{
    self.customerAttackDic = attackDic;
    
    NSArray *customerArr = attackDic[@"detail"];
    
    NSMutableArray *arr = [[NSMutableArray alloc]init];
    
    [arr addObject:@{@"title":[NSString stringWithFormat:@"所在价值链:%@",attackDic[@"memberGroup"]],@"subTitle":@""}];
    [arr addObject:@{@"title":[NSString stringWithFormat:@"客户类型:%@",attackDic[@"type"]],@"subTitle":@""}];
    NSDictionary *dic = [NSDictionary dictionaryWithDictionary:customerArr[0]];
    [arr addObject:@{@"title":[NSString stringWithFormat:@"%@:%@",dic[@"type"],dic[@"name"]],@"subTitle":[NSString stringWithFormat:@"%@",dic[@"content"]]}];
    NSDictionary *dics = customerArr[1];
    [arr addObject:@{@"title":[NSString stringWithFormat:@"%@:%@",dics[@"type"],dics[@"name"]],@"subTitle":[NSString stringWithFormat:@"%@",dics[@"content"]]}];
    NSDictionary *dicss = customerArr[2];
    [arr addObject:@{@"title":[NSString stringWithFormat:@"%@:%@",dicss[@"type"],dicss[@"name"]],@"subTitle":[NSString stringWithFormat:@"%@",dicss[@"content"]]}];
    
    for (int i = 0; i < arr.count; i ++) {
        AttackModel *model = [[AttackModel alloc]init];
        NSDictionary *dic = arr[i];
        model.title = dic[@"title"];
        model.subtitle = dic[@"subTitle"];
        [self.attackArr addObject:model];
    }
}
@end
